package com.surprise.db.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.SingleColumnRowMapper;
import org.springframework.stereotype.Repository;

import com.google.common.collect.Multimap;
import com.surprise.db.po.Store;

/**
 * StoreDAO.java
 * @author nick
 * 2015年3月2日
 */
@Repository
public class StoreDAO extends SimpleDAO<Store> {
    public final String columns = "(uid,iid,type,addtime)";
    public final String insertStatement = "(?,?,?,now())";

    public static class StoreSetter implements BatchPreparedStatementSetter {
        private List<Store> stores;

        public StoreSetter(List<Store> stores) {
            this.stores = stores;
        }

        @Override
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            Store store = stores.get(i);
            int index = 1;
            ps.setLong(index++, store.getUid());
            ps.setLong(index++, store.getIid());
            ps.setInt(index++, store.getType());
        }

        @Override
        public int getBatchSize() {
            return stores.size();
        }
    }

    public void add(List<Store> stores) {
        StringBuilder sb = new StringBuilder();
        sb.append("insert ignore into ")
            .append(getTableName())
            .append(columns)
            .append(" values ")
            .append(insertStatement);
        String sql = sb.toString();
        jdbcTemplate.batchUpdate(sql, new StoreSetter(stores));
    }

    public int del(int type, Multimap<Long, Long> uiMap) {
        if (uiMap == null || uiMap.size() == 0) {
            return 0;
        }
        StringBuilder sb = new StringBuilder();
        sb.append("select id from ")
            .append(getTableName())
            .append(" where type = ? and (");
        for (Long uid : uiMap.keySet()) {
            Collection<Long> iids = uiMap.get(uid);
            if (CollectionUtils.isNotEmpty(iids)) {
                String iidStr = StringUtils.join(iids, ',');
                sb.append("(uid = ").append(uid)
                    .append(" and iid in (")
                    .append(iidStr)
                    .append(")) or ");
            }
        }
        int index = sb.lastIndexOf("or");
        sb.delete(index, index + 2);
        sb.append(") ");
        String sql = sb.toString();

        List<Long> ids = jdbcTemplate.query(sql, new Object[] {
            type
        }, new SingleColumnRowMapper(Long.class));
        return del(ids);
    }

    public int del(Collection<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return 0;
        }
        String idStr = StringUtils.join(ids, ',');
        StringBuilder sb = new StringBuilder();
        sb.append("delete from ")
            .append(getTableName())
            .append(" where id in (")
            .append(idStr)
            .append(") ");
        String sql = sb.toString();
        return jdbcTemplate.update(sql);
    }
}
